package com.jeejam.order.service.impl;

import java.util.Date;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;


import com.jeejam.cart.pojo.Cart;
import com.jeejam.cart.pojo.CartItem;
import com.jeejam.framework.base.service.impl.BaseServiceImpl;
import com.jeejam.order.dao.OrderMapper;
import com.jeejam.order.pojo.Order;
import com.jeejam.order.pojo.OrderItem;
import com.jeejam.order.service.OrderItemService;
import com.jeejam.order.service.OrderService;

@Service
public class OrderServiceImpl extends BaseServiceImpl implements OrderService{
	private OrderMapper orderMapper;
	
	private OrderItemService orderItemService;

	@Resource
	public void setOrderMapper(OrderMapper orderMapper) {
		this.orderMapper = orderMapper;
		this.baseMapper = orderMapper;
	}

	@Resource
	public void setOrderItemService(OrderItemService orderItemService) {
		this.orderItemService = orderItemService;
	}

	@Override
	public Order buildOrder(Cart cart) {
		Order order = new Order();
		order.setProductAmount(cart.getTotalAccount());
		order.setOrderAmount(cart.getTotalAccount());
		order.setUserId(cart.getUserId());
		order.setCreateTime(new Date());
		orderMapper.insert(order);
		for(CartItem item : cart.getItems()){
			OrderItem orderItem = new OrderItem();
			orderItem.setOrderId(order.getId());
			orderItem.setProductId(item.getProductId());
			orderItem.setProductCode(item.getProduct().getCode());
			orderItem.setProductName(item.getProduct().getName());
			orderItem.setProductNumber(item.getCount());
			orderItem.setMarketPrice(item.getProduct().getMarketPrice());
			orderItem.setShopPrice(item.getProduct().getShopPrice());
			orderItem.setIsReal(item.getProduct().getIsReal());
			orderItemService.save(orderItem);
		}
		return orderMapper.select(order.getId());
	}

}
